<head{{ if .Site.Params.openGraph }} prefix="og: http://ogp.me/ns# article: http://ogp.me/ns/article#"{{ end }}>
    <meta charset="UTF-8" />

    {{ hugo.Generator }}

    {{- with .Site.Params.defaultTheme | default "light" -}}
        {{- if eq . "light" -}}
            <meta name="theme-color" content="{{ $.Site.Params.themeColor }}" />
        {{- else -}}
            <meta name="theme-color" content="{{ $.Site.Params.themeColorDark }}" />
        {{- end -}}
    {{- end -}}

    <!-- Dark Mode -->
    {{- if .Site.Params.enableDarkMode }}
        <script>
            const userPrefers = localStorage.getItem('theme');
            const darkModeMediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
            const lightModeMediaQuery = window.matchMedia('(prefers-color-scheme: light)');
            if (userPrefers === 'dark') {
                changeModeMeta('dark');
            } else if (userPrefers === 'light') {
                changeModeMeta('light');
            } else if (darkModeMediaQuery.matches) {
                changeModeMeta('dark');
            } else if (lightModeMediaQuery.matches) {
                changeModeMeta('light');
            }
            function changeModeMeta(theme) {
                document.documentElement.setAttribute('data-theme', theme);
                if (theme === 'dark') {
                    changeThemeColor('{{ .Site.Params.themeColorDark }}');
                } else {
                    changeThemeColor('{{ .Site.Params.themeColor }}');
                }
            }
            function changeThemeColor(themeColor) {
                document.querySelector('meta[name="theme-color"]').setAttribute('content', themeColor);
            }
        </script>
    {{- end }}

    <!-- Force HTTPS Redirection -->
    {{- if and .Site.Params.enableForceHTTPS (eq hugo.Environment "production") }}
        {{ $url := urls.Parse .Site.BaseURL }}
        {{ $host := $url.Host }}
        <script>
            if (window.location.host == "{{ $host | safeJS }}" && window.location.protocol != "https:") {
                window.location.protocol = "https";
            }
        </script>
    {{- end }}

    <!-- Responsive Web Design -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, user-scalable=yes" />
    <!-- Avoid Automatic Format Detection -->
    <meta name="format-detection" content="telephone=no, date=no, address=no, email=no" />
    <!-- Avoid Transcoding -->
    <meta http-equiv="Cache-Control" content="no-transform" />
    <!-- Avoid Baidu Mobile Search Transcoding -->
    <meta http-equiv="Cache-Control" content="no-siteapp" />

    {{ partial "utils/title.html" . }}
    {{- $title := .Scratch.Get "title" -}}
    <title>{{ $title }}</title>

    <!-- CSS -->
    {{- partial "style.html" . }}

    <!-- Fonts -->
    {{- with .Site.Params.fontsLink -}}
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />

        <link rel="stylesheet" href="{{ . }}" media="print" onload="this.media='all'" />
        <noscript><link rel="stylesheet" href="{{ . }}" /></noscript>
    {{- end }}

    <meta name="author" content="{{ .Params.author | default .Site.Author.name }}" />
    {{ partial "utils/summary.html" . }}
    {{- $description := .Description | default (.Scratch.Get "summary") | default .Site.Params.siteDescription | plainify -}}
    <meta name="description" content="{{ $description }}" />

    <!-- Favicon, Icons, Web App -->
    {{- $favicon := "favicon.ico" -}}
    {{- $safariMaskIcon := "icons/safari-pinned-tab.svg" -}}
    {{- $safariMaskColor := .Site.Params.safariMaskColor -}}
    {{- $appleTouchIcon := "icons/apple-touch-icon.png" -}}
    {{- partial "utils/relative-url.html" (dict "Deliver" . "filename" "") -}}
    {{- $url := .Scratch.Get "url" -}}
    {{- $msApplicationStartURL := $url -}}
    {{- $msApplicationTileColor := .Site.Params.msApplicationTileColor -}}
    {{- partial "utils/relative-url.html" (dict "Deliver" . "filename" "icons/mstile-150x150.png") -}}
    {{- $url := .Scratch.Get "url" -}}
    {{- $msApplicationTileImage := $url -}}
    {{- $manifest := "manifest.json" -}}
    <link rel="shortcut icon" href="{{ $favicon | relURL }}" type="image/x-icon" />
    <link rel="mask-icon" href="{{ $safariMaskIcon | relURL }}" color="{{ $safariMaskColor }}" />
    <link rel="apple-touch-icon" sizes="180x180" href="{{ $appleTouchIcon | relURL }}" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-title" content="{{ .Site.Title }}" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
    <meta name="mobile-web-app-capable" content="yes" />
    <meta name="application-name" content="{{ .Site.Title }}" />
    <meta name="msapplication-starturl" content="{{ $msApplicationStartURL }}" />
    <meta name="msapplication-TileColor" content="{{ $msApplicationTileColor }}" />
    <meta name="msapplication-TileImage" content="{{ $msApplicationTileImage }}" />
    <link rel="manifest" href="{{ $manifest | relURL }}" />

    <!-- Atom / RSS -->
    {{ with .OutputFormats.Get "SectionsAtom" -}}
        {{ printf `<link rel="%s" type="%s" href="%s" title="%s" />` .Rel .MediaType .Permalink $.Site.Title | safeHTML }}
    {{ end -}}
    {{ with .OutputFormats.Get "SectionsRSS" -}}
        {{ printf `<link rel="%s" type="%s" href="%s" title="%s" />` .Rel .MediaType .Permalink $.Site.Title | safeHTML }}
    {{ end }}

    <!-- SEO -->
    <link rel="canonical" href="{{ .Permalink }}" />
    {{- $Deliver := . -}}
    <!-- JSON-LD -->
    {{- with .Site.Params.jsonLD -}}
        {{ partial "utils/json-ld.html" (dict "Deliver" $Deliver "description" $description) }}
    {{- end }}

    <!-- Twitter Cards -->
    {{- with .Site.Params.twitterCards -}}
        {{ partial "utils/twitter-cards.html" (dict "Deliver" $Deliver) }}
    {{- end }}
    <!-- Open Graph -->
    {{- with .Site.Params.openGraph -}}
        {{ partial "utils/open-graph.html" (dict "Deliver" $Deliver "description" $description) }}
    {{- end }}

    {{- with .Site.Params.googleSiteVerification }}
        <meta name="google-site-verification" content="{{ . }}" />
    {{- end }}

    {{- if and .Site.Params.enableGoogleAnalytics (eq hugo.Environment "production") }}
        <link rel="preconnect" href="https://www.google-analytics.com" crossorigin />

        {{ partial "third-party/google-analytics.html" . }}
    {{- end }}

    {{- if or .Site.Params.enableGoogleAutoAds .Site.Params.enableGoogleAdUnits }}
        {{ if eq hugo.Environment "production" }}
            {{ partial "third-party/google-adsense.html" . }}
        {{ end }}
    {{- end }}

    {{- if and .Site.Params.enableGoogleAutoAds (eq hugo.Environment "production") -}}
        {{ partial "third-party/google-adsense-auto.html" . }}
    {{- end }}

    {{ partial "custom/head.html" . }}
</head>
